# coding: UTF-8
# Filename : compare.py
# Company : imedia
# Author : hui
# -*-coding:utf-8 -*-

from api.common.common import *
from api.common import mysql,log,utils,httputils,const,xml_tpl,err_tpl

def clist(request):
    httputils.footprint(request)
    
    cus_id = httputils.post(request, 'cus_id')
    size = httputils.post(request, 'size')
    pagenum = httputils.post(request, 'pagenum')
    
    if not utils.is_num(size): size = 5
    if not utils.is_num(pagenum): pagenum = 1
    if not utils.is_num(cus_id): cus_id = const.cus_id

    if validate(request):
        return validate(request)
    
    output = xml_tpl.imedia_start 
    output += make_cnodes(cus_id, size, pagenum)
    output += xml_tpl.imedia_end
    return response(output)

#生成多行记录xml节点
def make_cnodes(cus_id, size, pagenum):
    sql = '''
    select id, type, last_update from chain_compare where cus_id=%s
    limit %s, %s
    '''
    
    start_row = utils.get_star_row(size, pagenum)
    param = [int(cus_id), start_row, int(size)]
    result = mysql.doSearch(sql, param)
    return node_var_fill(result)

#节点变量填充    
def node_var_fill(result):
    body_str = ''
    for obj in result:
        compare_node = xml_tpl.compare_node
        compare_node = compare_node.replace('{id}', str(obj[0]))
        compare_node = compare_node.replace('{type}', str(obj[1]))
        compare_node = compare_node.replace('{last_update}', str(obj[2]))
        body_str += compare_node
    return body_str

    
